// pages/explore/index.js
// var {$http,$get} = require('../../utils/http')
let page = 0 //分页页码
Page({

    /**
     * 页面的初始数据
     */
    data: {
      tabArr:[
        // {icon:'renwen',catename:'正念冥想'},
        // {icon:'yejing',catename:'睡眠助手'},
        // {icon:'yinle',catename:'舒缓声音',children:[{}]}
      ],
      tabIdx:0,
      artlist:[],
      tabLeft:0,
      pageName:'探索',
      active:0,   //设置激活的子菜单
      
    },
    async onLoad(options) {
        //加载分类
        let res = await getApp().fly.get('/classes/LearnCate')
        let {results} = res.data
        let cateLv1 = results.filter(item=>item.father[0]=='100')
        cateLv1.forEach(item=>{
          let child = results.filter(itm=>itm.father[0]==item.objectId)
          item.children = [{catename:'全部'},...child]
        })
        this.setData({
          tabArr:cateLv1
        })
        //加载数据列表
        this.fetchData(cateLv1[0].objectId)
        
    },
    handleTitle(ev){ //测试子父通信
      console.log(ev);
      this.setData({
        pageName:ev.detail
      })
    },
    handleTab(ev){ //主菜单
      let {idx,id} = ev.currentTarget.dataset
      this.tabScoll(ev,idx) //菜单切换交互
      this.setData({active:0,artlist:[]})  //重置激活的子菜单
      page=0
      this.fetchData(id) //根据不同主分类id，加载不同数据
    },
    handleChildTab(ev){ //子菜单
      console.log(ev);
      let {tabIdx,tabArr} = this.data
      let {index} = ev.detail  //被选中二级菜单下标
      let catelv1 = tabArr[tabIdx].objectId //提取著分类id
      let catelv2 = tabArr[tabIdx].children[index].objectId //提取子分类id
      this.setData({active:index,artlist:[]})
      page=0
      this.fetchData(catelv1,catelv2)
    },
    tabScoll(ev,idx){
      let {offsetLeft} = ev.currentTarget //当前被点击按钮，距离左侧的偏移量
      let {tabIdx,tabLeft} = this.data
      if(idx>tabIdx){
          tabLeft = offsetLeft + 100
      }
      if(idx<tabIdx){
        tabLeft = offsetLeft - 100
      }
      console.log(idx,tabIdx,tabLeft);
      this.setData({
          tabIdx:idx,
          tabLeft
      })
    },
    fetchData(catelv1,catelv2){  //用以请求数据的函数
      let strJson = JSON.stringify({catelv1,catelv2})
      console.log(strJson);
      // 条数 limit
      // 偏移量 skip
      let skip = page*8
      getApp().fly.get(`/classes/LearnArticle?where=${strJson}&limit=8&skip=${skip}`).then(res=>{
          wx.stopPullDownRefresh() //关闭下拉刷新动画
          let {artlist} = this.data //原数据
          let {results} = res.data //新数据
          if(results.length){
            this.setData({
                artlist:[...artlist,...results]
            })
            page++
            return
          }
          wx.showToast({
            title: '没有更多数据了~',
            icon:'none'
          })

      })
    },
    handleDetail(ev){ //打开详情页
        let {idx,item} = ev.currentTarget.dataset
        wx.setStorage({ 
            key:'detail',
            data:item,
            success:()=>{
                wx.navigateTo({
                    url: '/pages/course-detail/index',
                })
            }
        })
        
    },
    onReachBottom(){ //触底函数
      console.log('触底了');
      let {tabIdx,tabArr,active} = this.data
      console.log(tabIdx,active);
      let catelv1 = tabArr[tabIdx].objectId //提取著分类id
      let catelv2 = tabArr[tabIdx].children[active].objectId //提取子分类id
      this.fetchData(catelv1,catelv2)
    },
    onPullDownRefresh(){  //下拉刷新，恢复到原始状态
      console.log('下拉刷新');
      let {tabArr} = this.data
      page=0
      this.setData({tabIdx:0,active:0,tabLeft:0,artlist:[]})
      let catelv1 = tabArr[0].objectId //提取著分类id
      let catelv2 = tabArr[0].children[0].objectId //提取子分类id
      this.fetchData(catelv1,catelv2)
    }
})